package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.content.Context;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.util.view.UniversalPhotoPop;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.CommodityManagementModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.UniversalModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/2.
 * 添加服务 家政
 */

public class AddTheServiceActivity extends BaseActivity implements View.OnClickListener , Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.service_category_layout)//选择分类
    LinearLayout service_category_layout;
    @Bind(R.id.service_category_text)//分类文字
            TextView service_category_text;
    @Bind(R.id.service_profile)//简介
            EditText service_profile;

    @Bind(R.id.service_price)//钱
            EditText service_price;


    @Bind(R.id.area_and_direction)//面积和方向
    LinearLayout area_and_direction;

    @Bind(R.id.service_area)//面积
            EditText service_area;

    @Bind(R.id.service_direction_layou)//选择方向
    LinearLayout service_direction_layou;
    @Bind(R.id.service_direction_text)//方向文字
            TextView service_direction_text;

    @Bind(R.id.service_price_layout)//选择单位
            LinearLayout service_price_layout;
    @Bind(R.id.service_price_text)//单位文字
            TextView service_price_text;

    @Bind(R.id.button)//提交
            Button button;
    @Bind(R.id.title_center)
    TextView title_center;

    Context context;
    String pos = "0"; //0、添加商品  1、修改商品 2、展示商品
    String u_id = "";//商家ID  pos=0时 u_id 为店铺id   pos=1时 u_id 为商品id  pos=2时 u_id 为商品id
    String merchanttype = "";//类型
    String typeid = "";//类型id
    String directionid = "";//方向id

    private MyProgressDialog mDialog;
    String Judge_interface = "0";//判断哪个接口   0、获取类型 1、上传
    int Judge_img = 0;// 类型是否点击
    List<UniversalModel> getclasstype = new ArrayList<>();//分类
    List<UniversalModel> getclassdirection= new ArrayList<>();
    boolean Judge_type = false;//服务类别点击
    boolean Judge_direction = false;
    private View viewkre;
    List<CommodityManagementModel> buycarlist = new ArrayList<>();
    int ints = 0;
    int type_direction = 0;//0、点击的是类型   1、点击的是分类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewkre = View.inflate(this, R.layout.activity_add_the_service, null);
        setContentView(viewkre);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        button.setOnClickListener(this);
        service_direction_layou.setOnClickListener(this);
        service_price_layout.setOnClickListener(this);
        service_category_layout.setOnClickListener(this);
        MineFragmentModel mineFragmentModel = UserInfoUtils.readUserInfo(this);
        merchanttype = mineFragmentModel.getMerchanttype();
        if (getIntent() != null) {
            pos = getIntent().getStringExtra("pos");
            u_id = getIntent().getStringExtra("id");
            if (!pos.equals("0")) {
                ints = getIntent().getIntExtra("ints", 0);
                buycarlist = (List<CommodityManagementModel>) getIntent().getSerializableExtra("data");
            }
        }
        if (pos.equals("1")) {
            title_center.setText("修改商品");
            button.setText("修改");
            processdetails();
        } else if (pos.equals("2")) {
            title_center.setText("商品详情");
            button.setVisibility(View.GONE);
            processdetails();
            Judge_img = 1;
            processnNotEdit();
        }
        if (!pos.equals("2")) {
            processclasstype();
        }

    }

    //展示产品详情
    private void processdetails() {
        if (buycarlist != null|| buycarlist.size() > 0) {
            CommodityManagementModel ManagementModel = buycarlist.get(ints);
            service_category_text.setText(ManagementModel.getClassname());
            service_profile.setText(ManagementModel.getSynopsis());
            service_price.setText(ManagementModel.getPrice());
            service_price_text.setText(ManagementModel.getServicetime());
            service_direction_text.setText(ManagementModel.getDirectionidname());
            service_area.setText(ManagementModel.getProportion());
            typeid = ManagementModel.getClassid();
          directionid= ManagementModel.getDirectionid();
            if (ManagementModel.getClassname().equals("家政")){
                area_and_direction.setVisibility(View.VISIBLE);
            }else {
                area_and_direction.setVisibility(View.GONE);
            }
        }
    }

    //不可编辑
    public void processnNotEdit() {
        service_profile.setEnabled(false);
        service_price.setEnabled(false);
        service_area.setEnabled(false);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.service_category_layout://类型选择
                if (Judge_img == 0) {
                    type_direction=0;
                    if (getclasstype.size() > 0) {
                        processtype();
                    } else {
                        Judge_type = true;
                        processclasstype();
                    }
                }
                break;
            case R.id.service_direction_layou://类型选择
                if (Judge_img == 0) {
                    type_direction=1;
                    if (getclassdirection.size() > 0) {
                        processdirection();
                    } else {
                        Judge_direction = true;
                        processclasstype();
                    }
                }
                break;
            case R.id.service_price_layout://单位选择
                if (Judge_img == 0) {
                    processunit();
                }
                break;

            case R.id.button://提交商品
                if (TextUtils.isEmpty(service_category_text.getText().toString().trim())) {//商品分类
                    ToastUtils.show(context, getResources().getString(R.string.please_service_classification));
                    return;
                }
                if (TextUtils.isEmpty(service_profile.getText().toString().trim())) {//商品简介
                    ToastUtils.show(context, getResources().getString(R.string.please_service_introduction));
                    return;
                }
                if (TextUtils.isEmpty(service_price.getText().toString().trim())) {//价格
                    ToastUtils.show(context, getResources().getString(R.string.please_service_price));
                    return;
                }

                if (service_category_text.getText().toString().trim().equals("家政")){
                    if (TextUtils.isEmpty(service_area.getText().toString().trim())) {//面积
                        ToastUtils.show(context, getResources().getString(R.string.please_service_area));
                        return;
                    }
                    if (TextUtils.isEmpty(service_direction_text.getText().toString().trim())) {//方向
                        ToastUtils.show(context, getResources().getString(R.string.please_service_direction));
                        return;
                    }
                }

                processupload();
                break;
        }
    }
    //绑定分类
    private void processtype() {
        UniversalPhotoPop typephoto = new UniversalPhotoPop(context, getclasstype, 1);
        typephoto.showAtLocation(viewkre, Gravity.BOTTOM, 0, 0);
        typephoto.setAddresskListener(new UniversalPhotoPop.OnAddressCListener() {
            @Override
            public void onClick(String province, String city, String area) {
                service_category_text.setText(province);
                for (int i = 0; i < getclasstype.size(); i++) {
                    if (province.equals(getclasstype.get(i).getName())) {
                        typeid = getclasstype.get(i).getId();
                    }
                }
                if (province.equals("家政")){
                    area_and_direction.setVisibility(View.VISIBLE);
                }else {
                    area_and_direction.setVisibility(View.GONE);
                }
            }


        });
    }
    //绑定方向
    private void processdirection() {
        UniversalPhotoPop typephoto = new UniversalPhotoPop(context, getclassdirection, 1);
        typephoto.showAtLocation(viewkre, Gravity.BOTTOM, 0, 0);
        typephoto.setAddresskListener(new UniversalPhotoPop.OnAddressCListener() {
            @Override
            public void onClick(String province, String city, String area) {
                service_direction_text.setText(province);
                for (int i = 0; i < getclassdirection.size(); i++) {
                    if (province.equals(getclassdirection.get(i).getName())) {
                        directionid = getclassdirection.get(i).getId();
                    }
                }
            }
        });
    }
    //绑定单位
    private void processunit() {
        List<UniversalModel> unit= new ArrayList<>();
        String[] names = getResources().getStringArray(R.array.number);
        UniversalModel model;
        for (int i=0;i<names.length;i++){
            model=new UniversalModel();
            model.setName(names[i]);
            unit.add(model);
        }
        UniversalPhotoPop typephoto = new UniversalPhotoPop(context, unit, 1);
        typephoto.showAtLocation(viewkre, Gravity.BOTTOM, 0, 0);
        typephoto.setAddresskListener(new UniversalPhotoPop.OnAddressCListener() {
            @Override
            public void onClick(String province, String city, String area) {
                service_price_text.setText(province);
            }
        });
    }
    //商品分类
    private void processclasstype() {
        if (Sundry.isOpenNetwork(context)) {

            mDialog = new MyProgressDialog(context);
            Judge_interface = "0";
            HttpClient.classtype(Constant.HOUSEKEEPINGGENRE, this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    //商品上传
    private void processupload() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_interface = "1";
            String data = "";

            if (pos.equals("0")) {//产品上传
                data = Constant.HOUSEKEEPINGADDSERVICE;
            } else {
                data = Constant.HOUSEKEEPINGUPDATE;//产品修改
            }


            HttpClient.addservice(data, pos, u_id, typeid, service_profile.getText().toString().trim(),
                    directionid, service_price.getText().toString().trim(), service_price_text.getText().toString().trim(),
                    service_area.getText().toString().trim() , this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }


    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result" + result);
        List<MerchantsTypeModel> getnote = JsonTools.getmerchanttype(result);
        if (getnote != null || getnote.size() > 0) {
            String error = getnote.get(0).getError();
            String mssage = getnote.get(0).getMssage();
            if (error.equals("200")) {
                String content = getnote.get(0).getResult();
                if (Judge_interface.equals("0")) {//商品分类
                    List<UniversalModel> getgenretype = JsonTools.getgenretype(content);
                    if (getgenretype!=null||getgenretype.size()>0){
                        String classification = getgenretype.get(0).getClassification();
                        String direction = getgenretype.get(0).getDirection();
                        getclasstype = JsonTools.getclasstype(classification);
                        getclassdirection = JsonTools.getclasstype(direction);
                        if (type_direction==0){
                            if (Judge_type) {
                                processtype();
                            }
                        }else {
                            if (Judge_direction) {
                                processdirection();
                            }
                        }
                    }

                } else if (Judge_interface.equals("1")) {//上传修改
                    finish();
                    ToastUtils.show(context, mssage);
                }
            } else if (error.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, mssage);
            } else {
                ToastUtils.show(context, mssage);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(Callback.CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
